package cn.maszy.service.impl.projectmanagement;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineerCategoryMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineerPositionMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineeringMapper;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategory;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategoryExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerPosition;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerPositionExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_Engineering;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerPositionExample.Criteria;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineerPositionService;
@Service
public class B_T_Ufp_EngineerPositionServiceImpl implements B_T_Ufp_EngineerPositionService {
	@Autowired
	private B_T_Ufp_EngineerPositionMapper b_T_Ufp_EngineerPositionMapper;
	@Autowired
	private B_T_Ufp_EngineerCategoryMapper b_T_Ufp_EngineerCategoryMapper;
	@Autowired
	private B_T_Ufp_EngineeringMapper b_T_Ufp_EngineeringMapper;
	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#update(cn.maszy.model.investment.B_T_Ufp_EngineerPosition, cn.maszy.model.investment.B_T_Ufp_AdminExample)
	 */
	public int update(B_T_Ufp_EngineerPosition b_T_Ufp_EngineerPosition) {
		// TODO Auto-generated method stub
		 if(b_T_Ufp_EngineerPosition.getUuid()==null||"".equals(b_T_Ufp_EngineerPosition.getUuid())){
			 b_T_Ufp_EngineerPosition.setUuid(UUID.randomUUID().toString());
			 b_T_Ufp_EngineerPosition.setIsDelete(0);
			 b_T_Ufp_EngineerPosition.setCreateTime(new Date());
			 b_T_Ufp_EngineerPosition.setCreateUser(SessionFactory.getSessionUserRealNam());
			 return b_T_Ufp_EngineerPositionMapper.insert(b_T_Ufp_EngineerPosition);
		 }else{
			 b_T_Ufp_EngineerPosition.setUpdateTime(new Date());
			 b_T_Ufp_EngineerPosition.setUpdateUser(SessionFactory.getSessionUserRealNam());
			 return b_T_Ufp_EngineerPositionMapper.updateByPrimaryKeySelective(b_T_Ufp_EngineerPosition);
		 }
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#selectOne(java.lang.String)
	 */
	public B_T_Ufp_EngineerPosition selectOne(String uuid) {
		// TODO Auto-generated method stub
		B_T_Ufp_EngineerPosition record=b_T_Ufp_EngineerPositionMapper.selectByPrimaryKey(uuid);
		return record;
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#delete(java.lang.String)
	 */
	public int delete(String checkIds) {
		// TODO Auto-generated method stub
		for (String id : checkIds.split(", ")) {
			//获取删除数据的对象，后面要使用者个对象的工程uuid算总金额
			B_T_Ufp_EngineerPosition record=b_T_Ufp_EngineerPositionMapper.selectByPrimaryKey(id);
			b_T_Ufp_EngineerPositionMapper.deleteByPrimaryKey(id);
			//联动删除项目详细表
			B_T_Ufp_EngineerCategoryExample example=new B_T_Ufp_EngineerCategoryExample();
			example.createCriteria().andPositionUuidEqualTo(id);
			b_T_Ufp_EngineerCategoryMapper.deleteByExample(example);
			//还修改项目总金额；
			//根据uuid 查询到所属的工程id
			float price =totalall(record);
			B_T_Ufp_Engineering b_T_Ufp_Engineering =new B_T_Ufp_Engineering();
			b_T_Ufp_Engineering.setUuid(record.getEngineeringUuid());
			b_T_Ufp_Engineering.setEngineeringPrice(price);
			b_T_Ufp_EngineeringMapper.updateByPrimaryKeySelective(b_T_Ufp_Engineering);
		}
		return 1;
	}
	//根据工程id计算总金额
		public float totalall(B_T_Ufp_EngineerPosition b_T_Ufp_EngineerPosition)
		{
			B_T_Ufp_EngineerCategoryExample example=new B_T_Ufp_EngineerCategoryExample();
			String sql = "SELECT SUM(total_amount)AS total_amount FROM b_t_ufp_engineercategory WHERE  engineering_uuid ='"+b_T_Ufp_EngineerPosition.getEngineeringUuid()+"'";
			example.setOrderByClause(sql);
			List<B_T_Ufp_EngineerCategory>  list=b_T_Ufp_EngineerCategoryMapper.selectByExampleby(example);
			Float i=(float) 0;
			if(list.get(0) == null)
			{
				return 0;
			}
			else
			{
				i = list.get(0).getTotalAmount();
			}
			return i;
		}
		
	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#count(cn.maszy.model.investment.B_T_Ufp_AdminExample)
	 */
	public int count(B_T_Ufp_EngineerPositionExample example,B_T_Ufp_EngineerPositionMapper b_T_Ufp_EngineerPositionMapper) {
		// TODO Auto-generated method stub
		return b_T_Ufp_EngineerPositionMapper.countByExample(example);
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#selectList(cn.maszy.model.investment.B_T_Ufp_EngineerPosition)
	 */
	public Pager<B_T_Ufp_EngineerPosition> selectList(B_T_Ufp_EngineerPosition b_T_Ufp_EngineerPosition,Pager<B_T_Ufp_EngineerPosition> pager) {
		// TODO Auto-generated method stub
		// 添加查询条件
		B_T_Ufp_EngineerPositionExample example=new B_T_Ufp_EngineerPositionExample();
		Criteria criteria = example.createCriteria();
		if(b_T_Ufp_EngineerPosition!=null){
//			if(b_T_Ufp_EngineerPosition.getEngineeringUuid()!=null&&!"".equals(b_T_Ufp_EngineerPosition.getEngineeringUuid())){
//				criteria.andEngineeringUuidEqualTo(b_T_Ufp_EngineerPosition.getEngineeringUuid());
//			}
//			if(b_T_Ufp_EngineerPosition.getStatus()!=null&&!"".equals(b_T_Ufp_EngineerPosition.getStatus())){
//				criteria.andStatusEqualTo(b_T_Ufp_EngineerPosition.getStatus());
//			}
//			if(b_T_Ufp_EngineerPosition.getRealName()!=null&&!"".equals(b_T_Ufp_EngineerPosition.getRealName())){
//				criteria.andRealNameLike("%"+B_T_Ufp_EngineerPosition.getRealName()+"%");
//			}
		}else{
			criteria.andUuidIsNotNull();
		}
		example.or(criteria);
		int totalCount=new B_T_Ufp_EngineerPositionServiceImpl().count(example,b_T_Ufp_EngineerPositionMapper);
		pager.setTotalRecords(totalCount);
		example.setLimitStart(pager.getStartRecord());
		example.setLimitEnd(pager.getPageSize());
		example.setOrderByClause(pager.getOrderField()+" "+pager.getOrderDirection());
		
		// 取得总的记录条数
		List<B_T_Ufp_EngineerPosition>  list=b_T_Ufp_EngineerPositionMapper.selectByExample(example);
		pager.setPageRecords(list);
		return pager;
	}
	public List<B_T_Ufp_EngineerPosition> select(B_T_Ufp_EngineerPosition b_T_Ufp_EngineerPosition)
	{
		B_T_Ufp_EngineerPositionExample example=new B_T_Ufp_EngineerPositionExample();
		Criteria criteria = example.createCriteria();
		if(b_T_Ufp_EngineerPosition!=null){
			if(b_T_Ufp_EngineerPosition.getEngineeringUuid()!=null&&!"".equals(b_T_Ufp_EngineerPosition.getEngineeringUuid())){
				criteria.andEngineeringUuidEqualTo(b_T_Ufp_EngineerPosition.getEngineeringUuid());
			}
			if(b_T_Ufp_EngineerPosition.getEngineeringName()!=null&&!"".equals(b_T_Ufp_EngineerPosition.getEngineeringName())){
				criteria.andEngineeringNameEqualTo(b_T_Ufp_EngineerPosition.getEngineeringName());
			}
			if(b_T_Ufp_EngineerPosition.getEngineeringType()!=null&&!"".equals(b_T_Ufp_EngineerPosition.getEngineeringType())){
				criteria.andEngineeringTypeEqualTo(b_T_Ufp_EngineerPosition.getEngineeringType());
			}
		}else
		{
			criteria.andUuidIsNotNull();
		}
		example.or(criteria);
		example.setOrderByClause("create_time");
		// 取得总的记录条数
		List<B_T_Ufp_EngineerPosition>  list=b_T_Ufp_EngineerPositionMapper.selectByExample(example);
		return list;
	}
}
